package com.pri.chat;

import android.Manifest;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.KeyEvent;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.FrameLayout;
import android.widget.RadioButton;
import android.widget.TextView;

import com.netease.nim.uikit.common.ToastHelper;
import com.netease.nim.uikit.support.permission.MPermission;
import com.netease.nim.uikit.support.permission.annotation.OnMPermissionDenied;
import com.netease.nim.uikit.support.permission.annotation.OnMPermissionGranted;
import com.netease.nim.uikit.support.permission.annotation.OnMPermissionNeverAskAgain;
import com.pri.chat.activity.OtherPeopleMsgDhActivity;
import com.pri.chat.activity.SearchMemberDhActivity;
import com.pri.chat.application.APP;
import com.pri.chat.fragment.MainFragment;
import com.pri.chat.fragment.PersonCenterFragment;
import com.pri.chat.fragment.RecentContactsFragment;
import com.pri.chat.fragment.VideoFragment1;
import com.pri.chat.fragment.WishFragment;
import com.pri.chat.fragment.WishMsgFragment;
import com.pri.chat.fragment.daohang.RecentContactsFragmentDh;
import com.pri.chat.nim.session.SessionHelper;
import com.pri.chat.view.XRadioGroup;
import com.vondear.rxtool.RxActivityTool;

import androidx.annotation.IdRes;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentTransaction;
import butterknife.BindView;
import butterknife.ButterKnife;


public class MainDaoHangActivity extends AppCompatActivity implements XRadioGroup.OnCheckedChangeListener {

    FragmentTransaction fTransaction;
    @BindView(R.id.ly_content)
    FrameLayout lyContent;
    @BindView(R.id.btn_main)
    RadioButton btnMain;
    @BindView(R.id.btn_video)
    RadioButton btnVideo;
    @BindView(R.id.btn_message)
    RadioButton btnMessage;

    @BindView(R.id.btn_shop)
    RadioButton btnShop;
    @BindView(R.id.btn_me)
    RadioButton btnMe;
    @BindView(R.id.unread_num)
    TextView unread_num;
    @BindView(R.id.rg_tab_bar)
    XRadioGroup rgTabBar;
    @BindView(R.id.lines)
    View lines;

    @BindView(R.id.clickBtn1)
    View clickBtn1;

    @BindView(R.id.clickBtn2)
    View clickBtn2;

    @BindView(R.id.viewM)
    View viewM;





    private FragmentManager fManager;
    private Fragment fg1;
    private Fragment fg2;
    private Fragment fg3;
    private Fragment fg4;
    private Fragment fg5;


    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main_dh_layout);
        ButterKnife.bind(this);
        initFragments(savedInstanceState != null);
        initView();
    }

    //对返回键进行监听
    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK && event.getRepeatCount() == 0) {
            return true;
        }
        return super.onKeyDown(keyCode, event);
    }

    private int checkid;

    @Override
    public void onCheckedChanged(XRadioGroup group, @IdRes int checkedId) {
        FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
        System.out.println(getSupportFragmentManager().getFragments().size() + "个fragment");
        hideAllFragment(ft);
        for (Fragment f : getSupportFragmentManager().getFragments()) {
            if (TextUtils.equals(f.getTag(), String.valueOf(checkedId))) {
                if(f instanceof WishMsgFragment){
                    ((WishMsgFragment)f).getWishMoney();
                }
                ft.show(f);
                break;
            }
        }
        ft.commitAllowingStateLoss();
    }

    //隐藏所有Fragment
    private void hideAllFragment(FragmentTransaction fragmentTransaction) {
        if (fg1 != null) fragmentTransaction.hide(fg1);
        if (fg2 != null) fragmentTransaction.hide(fg2);
        if (fg3 != null) fragmentTransaction.hide(fg3);
        if (fg4 != null) fragmentTransaction.hide(fg4);
        if (fg5 != null) fragmentTransaction.hide(fg5);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);

    }

    @Override
    public void onBackPressed() {
        super.onBackPressed();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
    }

    private void initFragments(boolean clearInfo) {
        fTransaction = getSupportFragmentManager().beginTransaction();
        if (clearInfo) {
            for (Fragment f : getSupportFragmentManager().getFragments()) {
                fTransaction.remove(f);
            }
        }
        fg1 = new WishFragment();
        fg2 = new VideoFragment1();
        fg3 = new RecentContactsFragmentDh();
        fg4 = new MainFragment();
        fg5 = new PersonCenterFragment();
        fTransaction.add(R.id.ly_content, fg1, String.valueOf(R.id.btn_shop))
                .add(R.id.ly_content, fg2, String.valueOf(R.id.btn_video))
                .add(R.id.ly_content, fg3, String.valueOf(R.id.btn_message))
                .add(R.id.ly_content, fg4, String.valueOf(R.id.btn_main))
                .add(R.id.ly_content, fg5, String.valueOf(R.id.btn_me))
                .commitNow();

    }

    private void initView() {
        SessionHelper.init();
        fManager = getSupportFragmentManager();
        rgTabBar.setOnCheckedChangeListener(this);
        //获取第一个单选按钮，并设置其为选中状态
        btnShop.setChecked(true);


        clickBtn1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                RxActivityTool.skipActivity(MainDaoHangActivity.this, OtherPeopleMsgDhActivity.class);
                finish();
            }
        });
        clickBtn2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                RxActivityTool.skipActivity(MainDaoHangActivity.this, OtherPeopleMsgDhActivity.class);
                finish();
            }
        });
        viewM.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                RxActivityTool.skipActivity(MainDaoHangActivity.this, OtherPeopleMsgDhActivity.class);
                finish();
            }
        });
    }
}
